package com.ghovos.usermangerpgdemo.common.exception;

import com.ghovos.usermangerpgdemo.common.result.GlobalResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;


/**
 * 统一处理controller中产生的异常
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public GlobalResult<Object> error(Exception e) {
        e.printStackTrace();
        return new GlobalResult<>() {{
            setMessage(e.getMessage());
            setCode(201);
        }};
    }

    @ResponseBody
    @ExceptionHandler(GlobalException.class)
    public GlobalResult<Object> error(GlobalException globalException) {
        globalException.printStackTrace();
        return new GlobalResult<>() {{
            setMessage(globalException.getMessage());
            setCode(globalException.getCode());
        }};
    }

}
